using System;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace UserInterface.UserControls
{
    public partial class Pager : System.Web.UI.UserControl
    {
        private int pageCount;
        private int currentPage;
        private string urlBase;

        public int PageCount
        {
            get { return pageCount; }
            set { pageCount = value; }
        }

        public int CurrentPage
        {
            get { return currentPage; }
            set { currentPage = value; }
        }

        public int PreviousPage
        {
            get { return currentPage - 1; }
        }

        public int NextPage
        {
            get { return currentPage + 1; }
        }
        
        public string UrlBase
        {
            get { return urlBase; }
            set { urlBase = value; }
        }

        public string UrlPrefix
        {
            get { return urlBase + (urlBase.Contains("?") ? "&amp;" : "?"); }
        }

        void Page_PreRender(object sender, EventArgs e)
        {
            List<int> pages = new List<int>();
            for (int page = 1; page <= pageCount; page++)
            {
                pages.Add(page);
            }
            PagesRepeater.DataSource = pages;
            PagesRepeater.DataBind();

            FirstPagePlaceHolder.Visible =
            PreviousPagePlaceHolder.Visible = currentPage > 1;

            LastPagePlaceHolder.Visible =
            NextPagePlaceHolder.Visible = currentPage < pageCount;
        }

        protected void PagesRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            int page = (int)e.Item.DataItem;
            PlaceHolder normalPagePlaceHolder = (PlaceHolder)e.Item.FindControl("NormalPagePlaceHolder");
            PlaceHolder currentPagePlaceHolder = (PlaceHolder)e.Item.FindControl("CurrentPagePlaceHolder");
            normalPagePlaceHolder.Visible = page != currentPage;
            currentPagePlaceHolder.Visible = page == currentPage;
        }
    }
}